<?php
/**
 * Class to create front models
 * @author muinx
 */
class CreateFrontModel
{
	protected $model;
	
	function __construct($model)
	{
		$this->model = $model;
	}
	
	/**
	 * Process model content
	 * @param String $content		Content to process
	 * @param String $filename		File name
	 * @param Object $component		Component Object
	 * @return String	Content was replaced
	 */
	function create($content, $filename, $component)
	{
		$model = $this->model;
		
		$comName = str_replace(' ', '_', strtolower($component->name));
		$componentPrefix = str_replace(' ', '_', ucwords($component->name));
		
		$modelFormName 	= null;
		$search 		= array();
		$replace 		= array();
		
		if(is_object($model))
			$modelFormName = str_replace(' ', '_', strtolower($model->name));
		
		$search = array('#table#', '#xs#', '#author#', '#to_copy#', '#component_prefix#', '#X_with_s#', '#TO_COPY#', '#XS#', '#X_no_s#');
		$replace = array($model->table_name, $filename, $component->author, $comName, $componentPrefix, ucfirst($filename), strtoupper($comName), strtoupper($filename), ucfirst($modelFormName));

		$joins = GenerateHelper::getRecord('#__gencode_genmfront_join', null, 'genmfront_id = ' . (int) $model->id);
		
		require_once JPATH_COMPONENT_ADMINISTRATOR . DS . 'models' . DS . 'genm.php';
		
		//select fields
		$genmFields = $this->getFieldSelected($model->id, null, $model->table_name);
		
		$search[] = '#SELECT_FIELDS#';
		$replace[] = ', ' .$genmFields;
		
		$genmFieldsConfig = $this->getFieldSelected($model->id, true, $model->table_name);
		
		$search[] = '#SELECT_FIELDS_CONFIG#';
		$replace[] = $genmFieldsConfig;
		
		$strJoin = '';
		
		foreach ($joins as $key => $join)
		{
			$joinTable = $join->join_table;
			$tableAlias = $joinTable.'_'.$key;
			
			$strJoin .= '// Join over the ' . $joinTable . "\n";
			
			$listField = array();
			$fields = explode(',', str_replace(' ', '', $join->join_table_fields));
			
			if(!empty($fields))
			{
				foreach ($fields as $field)
				{
					if($field)
						$listField[] = $tableAlias . '.' . $field . ' AS ' . $tableAlias . '_' . $field;
				}
			}
			
			if(!empty($listField))
			{
				$listField = implode(', ', $listField);
				$strJoin .= '$query->select(\''.$listField.'\');' . "\n";
			}
			
			$strJoin .= '$query->join(\''.$join->join_type.'\', \'#__'.$joinTable.' AS '.$tableAlias.' ON '.$tableAlias.'.'.$join->join_field_on_1.' = #__'.$join->join_table_on_2.'.'.$join->join_field_on_2.'\');' . "\n" . "\n";
		}
		
		$search[] = '#JOIN_TABLE_STRING#';
		$replace[] = $strJoin;
		
		$content = str_replace($search, $replace, $content);
		
		return $content;
	}
	
	/**
	 * Get fields in model
	 * @param Int $modelId				Model ID
	 * @param Bool $getConfig			If true, will set field for config
	 * @param String $modelTableName	Table name that model selected
	 * @return	List fields
	 */
	public function getFieldSelected($modelId, $getConfig = null, $modelTableName = null)
	{
		$db = JFactory::getDbo();
		$query = "SELECT field FROM #__gencode_genmfront_fields WHERE genmfront_id = '$modelId'";
		
		$db->setQuery($query);
		$rs = $db->loadResultArray();
		
		$fields = array();
		
		if($getConfig)
		{
			foreach ($rs as $rec)
				$fields[] = "'" . $rec . "', " . "'#__".$modelTableName."." . $rec . "'";
		}
		else 
		{
			foreach ($rs as $rec)
				$fields[] = "#__".$modelTableName."." . $rec . "";
		}
		
		$fields = implode(', ', $fields);
		
		return $fields;
	}
}